const httpclient = require('../../../utils/httpclient.js')
const md5Util = require('../../../utils/md5.js')
const util = require('../../../utils/util.js')

var app = getApp();
Page({
    data:{
      orderId:0,
      goodsList:[
      ],
      nextStateMassage: {
        2: "马上发货",
        3: "交易完成",
        4: ""
      },
    },
    updateState: function (e) {
      let eleData = e.currentTarget.dataset;
      let that = this;
      httpclient.req(
        "/manage/order/update",
        {
          id: eleData.id,
          state: Number(eleData.state) + 1
        },
        'post',
        function (res) {
          var result = res.data.result;
          if (res.data.code == 0) {
            wx.showToast({
              title: '操作成功',
              icon: 'success',
              duration: 2000
            });
            that.onLoad({ id: eleData.id});
          } else {
            wx.showToast({
              title: res.data.message,
              duration: 2000
            });
          }
        },
        function (result) {
          console.log(result)
        }
      );
    },
    onLoad:function(e){
      var orderId = e.id;
      var that = this;
      this.setData({
        orderId: orderId
      });
      httpclient.req(
        "/manage/order/detail",
        {
          id: orderId
        },
        'post',
        function (res) {
          console.log(res);
          that.setData({
            orderDetail: res.data.result.record,
            goodsList: res.data.result.record.details
          });

          // 如果状态为1代付款要计算当前时间与最后支付时间差
          if (res.data.state == 1) {
            var create_date = new Date(res.data.result.record.create_date.replace(/-/g, '/'));
            
            create_date.setTime(create_date.getTime() + 12 * 60 * 60 * 1000);
            var now = new Date();
            var date3 = create_date.getTime() - now.getTime()  //时间差的毫秒数
            //计算出小时数
            var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1 / (3600 * 1000))
            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2 / (60 * 1000))
            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
            var seconds = Math.round(leave3 / 1000)
            that.setData({
              hours: hours,
              minutes: minutes,
              seconds: seconds
            });

            var timer = setInterval(function(){
              var hours = that.data.hours, minutes = that.data.minutes, seconds = that.data.seconds;
              if (that.data.seconds > 0){
                seconds = parseInt(that.data.seconds)-1;
              } else if (that.data.minutes > 0) {
                minutes = parseInt(that.data.minutes) - 1;
                seconds = 59;
              } else if (that.data.hour > 0) {
                if (that.data.hours == 0){
                  clearInterval(timer);
                  // 刷新当前页面
                  that.onShow();
                }
                hours = parseInt(that.data.hours) - 1;
                minutes = 59;
                seconds = 59;
              }

              that.setData({
                hours: hours,
                minutes: minutes,
                seconds: seconds
              });
            },1000);
          }
        },
        function (result) {
          console.log(result)
        }
      );
      /** 
       * 获取系统信息 
       */
      wx.getSystemInfo({
        success: function (res) {
          var n = 750 / res.windowWidth;
          that.setData({
            winWidth: res.windowWidth,
            winHeight: res.windowHeight,
            scrollHeight: res.windowHeight - (87 / n)
          });
        }
      });
    },
    onShow : function () {
      var that = this;

      var yunPrice = parseFloat(this.data.yunPrice);
      var allprice = 0;
      var goodsList = this.data.goodsList;
      for (var i = 0; i < goodsList.length; i++) {
        allprice += parseFloat(goodsList[0].price) * goodsList[0].number;
      }
      this.setData({
        allGoodsPrice: allprice
      });
    },
})